package com.summer.data.table.column;

/**
 * @author 345480567@qq.com 时间，不带日期
 */

import com.summer.data.logging.Logger;
import com.summer.data.logging.LoggerFactory;
import com.summer.data.table.BaseColumn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.time.LocalTime;

/**
 * 时间，不带日期
 */
public class TimeColumn extends BaseColumn<LocalTime> {

    private static Logger logger = LoggerFactory.getLogger(TimeColumn.class);

    public TimeColumn(String colName) {
        super(colName);
    }

    @Override
    public void fetchNotNullValue(ResultSet resultSet, Integer index) throws SQLException {
        value = getValue(resultSet, index);
    }

    public static LocalTime getValue(ResultSet resultSet, Integer index) throws SQLException {
        LocalTime value = null;
        Object object = resultSet.getObject(index);
        if (object != null) {
            if (object instanceof LocalTime) {
                value = (LocalTime) object;
            } else if (object instanceof Time) {
                Time time = (Time)object;
                value = time.toLocalTime();
            } else {
                logger.warn("时间类型不匹配: db:%s  -> java:%s",
                        new Object[]{ object.getClass().getName(), LocalTime.class.getName() });
            }
        }
        return value;
    }
}

